package com.egao.common.manage.school;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.egao.common.core.Constants;
import com.egao.common.core.annotation.ApiPageParam;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.student.entity.vo.StudentListVO;
import com.egao.common.module.system.entity.*;
import com.egao.common.module.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lym
 * @date 2022/11/8 14:56
 */
@Api(tags = "辅导员管理")
@RestController
@RequestMapping("${api}")
public class SchoolTeacherController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private LoginRecordService loginRecordService;
    @Autowired
    private PushViewRecordService pushViewRecordService;

    @PreAuthorize("hasAuthority('school:teacher:list')")
    @ApiOperation(value = "分页查询辅导员")
    @GetMapping("/school/teacher/page")
    public PageResult<User> page(HttpServletRequest request) {
        Role role = roleService.getOne(new LambdaQueryWrapper<Role>().eq(Role::getRoleCode, "teacher"));
        if (role == null) {
            return new PageResult<>();
        }

        List<UserRole> userRoleList = userRoleService.list(new LambdaQueryWrapper<UserRole>().eq(UserRole::getRoleId, role.getRoleId()));
        if (userRoleList.size() == 0) {
            return new PageResult<>();
        }

        List<Integer> userIds = userRoleList.stream().map(UserRole::getUserId).collect(Collectors.toList());
        PageParam<User> pageParam = new PageParam<>(request);
        pageParam.put("userIds", userIds);
        pageParam.setDefaultOrder(null, new String[]{"create_time"});
        List<User> list = userService.listPage(pageParam);
        if (list.size() > 0) {
            // 登录记录
            List<String> usernameList = list.stream().map(User::getUsername).collect(Collectors.toList());
            List<LoginRecord> loginRecordList = loginRecordService.list(new LambdaQueryWrapper<LoginRecord>()
                    .select(LoginRecord::getUsername)
                    .eq(LoginRecord::getOperType, Constants.RESULT_OK_CODE)
                    .in(LoginRecord::getUsername, usernameList));

            // 推送信息查看记录
            List<Integer> userIdList = list.stream().map(User::getUserId).collect(Collectors.toList());
            List<PushViewRecord> pushViewRecordList = pushViewRecordService.list(new LambdaQueryWrapper<PushViewRecord>()
                    .select(PushViewRecord::getUserId)
                    .in(PushViewRecord::getUserId, userIdList));

            for (User user : list) {
                long count = loginRecordList.stream().filter(d -> d.getUsername().equals(user.getUsername())).count();
                user.setLoginCount((int) count);

                long count1 = pushViewRecordList.stream().filter(d -> d.getUserId().equals(user.getUserId())).count();
                user.setPushViewCount((int) count1);
            }
        }

        return new PageResult<>(list, pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('school:teacher:list')")
    @ApiOperation(value = "分页查询辅导员登录记录")
    @GetMapping("/school/teacher/login-record")
    public PageResult<LoginRecord> loginRecordPage(HttpServletRequest request) {
        PageParam<LoginRecord> pageParam = new PageParam<>(request);
        pageParam.put("operType", 0);
        pageParam.setDefaultOrder(null, new String[]{"create_time"});
        return new PageResult<>(loginRecordService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('school:teacher:list')")
    @ApiOperation(value = "分页查询辅导员查看推送记录")
    @GetMapping("/school/teacher/push-record")
    public PageResult<PushViewRecord> pushRecordPage(HttpServletRequest request) {
        PageParam<PushViewRecord> pageParam = new PageParam<>(request);
        pageParam.setDefaultOrder(null, new String[]{"create_time"});
        return new PageResult<>(pushViewRecordService.page(pageParam, pageParam.getWrapper()).getRecords(), pageParam.getTotal());
    }
}
